﻿using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using game.character.battle;
using lumo;
using lumo.display.object2D;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using lumo.display.animation2D;
using game.character.weapon;
using game.scene;

namespace game.character.enemy
{
    /// <summary>
    /// The Fish.
    /// </summary>
    public class EnemyShark : EnemyBase
    {

        protected const string _Texture = "Graphics/Enemies/Shark";
        protected const int    _Life = 20000;
        protected const float  _Hit  = 1000;
        protected lumo.display.light.Light _Light;

        /// <summary>
        /// Constructor for RedEnemy.
        /// </summary>
        public EnemyShark(LumoComponent component, GameMap map, Tiled.Object tiledObject)
            : base(component, map, tiledObject)
        {
            Battler = new GameEnemy(_Life);
            Battler.HitRate = _Hit;

            // Save the texture.
            Texture = component.Content.Load<Texture2D>(_Texture);
            Weapon = new EnemyWeapon(_LumoComponent.Content.Load<Texture2D>("Graphics/Weapons/Bullet01"));
        }

        /// <summary>
        /// Update the character.
        /// </summary>
        /// <param name="component">Update using lumo</param>
        protected override void CreateBody()
        {
            // Create Submarine Body.
            Body = BodyFactory.CreateRectangle(Global.Map.World, Texture.Width / 4 / Global.PhysicsScale, Texture.Height / Global.PhysicsScale, 0.125f);
            Body.BodyType = BodyType.Dynamic;
            Body.IsSensor = true;
            Body.IgnoreGravity = true;
            Body.CollisionCategories = Global.PhysicsCategory.Enemy;
            Body.CollidesWith = Global.PhysicsCategory.Actor | Global.PhysicsCategory.ActorBullet;
            Body.LinearDamping = 1.0f;
            Body.AngularDamping = 1.0f;
            Body.Restitution = 0.25f;

            _Light = _GameMap.LightManager.CreateLight(256, 4f);
            _Light.Position = this.ScreenPosition + new Vector2(16 * Global.AditionalTiles, 16 * Global.AditionalTiles);
        }

        /// <summary>
        /// Setup the Parameters
        /// </summary>
        protected override void SetupParameters()
        {
            // Setup The coordinates
            Position = new Vector2(_TiledObject.X, _TiledObject.Y);
            SourceRectangle = new Rectangle(0, 0, Texture.Width / 2, Texture.Height);
            ScreenOrigin = new Vector2(Texture.Width / 4, Texture.Height / 2);
        }

        /// <summary>
        /// Setup the Artificial Intelligency.
        /// </summary>
        protected override void SetupAI()
        {
            // Get Properties.
            float Speed = GetFloatProperty("Speed", 400f);
            float PointSize = GetFloatProperty("PointSize", 100f);
            float SightRange = GetFloatProperty("SightRange");
            float ShootDelay = GetFloatProperty("ShootDelay", 400.0f);

            // Delay actions
            AIOnTimer(ShootDelay, ShootDelay, () => "ShootTimer");

            // Clear the velocity.
            AIClearVelocity();

            // The route patter of the fish
            if (_TiledObject.GeometryType == Tiled.Object.GeometryTypes.Line)
                AIFixedMoviment(_TiledObject.GetAbsolutePoints(true), Speed, PointSize);

            //
            bool[] ShootValid = new bool[] { false };
            // Follow On Range
            if (SightRange > 0)
            {

                AIOnRange(
                    SightRange,
                    _GameMap.Player,
                    () =>
                    {
                        ShootValid[0] = true;
                        return null;
                    }
                );
            }


            // Animation.
            FrameAnimation2D Animation = null;
            AIOnUpdate(
                () =>
                {
                    if (ShootValid[0])
                    {
                        Weapon.Shoot(BulletManager, Position, _GameMap.Player.Position, Vector2.Zero);
                        Animation = new FrameAnimation2D(2, 1, new uint[] { 1, 0 }, 150.0f, Smooth.None, Loop.None, this);
                    }
                    return null;
                }
            ).OnGroups("ShootTimer");

            AIOnUpdate(
                () =>
                {
                    if ( Animation != null )
                        Animation.Update(_LumoComponent);
                    _Light.Position = this.ScreenPosition + new Vector2(16 * Global.AditionalTiles, 16 * Global.AditionalTiles);
                    return null;
                }
            );

            // Turn According to Position
            AIOnCondition(
                () => _GameMap.Player.Position.X <= this.Position.X,
                () =>
                {
                    ScreenMirrorX = true;
                    return null;
                },
                () =>
                {
                    ScreenMirrorX = false;
                    return null;
                }
            );

            // Damage the object on contact
            AIDamageOnContact(_GameMap.Player, "Damaging");
        }

        protected override void SetupDead()
        {
            base.SetupDead();
            _LumoComponent.Scene = new SceneCredits(_LumoComponent.Scene);
        }

    }

}